package cn.smile.utils;

import cn.smile.config.mail.EmailConfig;
import cn.smile.entity.User;
import freemarker.template.Template;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMailMessage;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Component;
import org.springframework.ui.freemarker.FreeMarkerTemplateUtils;
import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.File;
import java.util.*;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author: MaoSuyu
 * @User：John
 * @Date: 2019/5/29
 * @Time: 22:00
 * @Description: No Description
 */
@Component
public class EmailService {

    @Autowired
    private EmailConfig emailConfig;
    @Autowired
    private JavaMailSender javaMailSender;
    @Autowired
    private FreeMarkerConfigurer freeMarkerConfigurer;

    /**
     * 普通的邮件发送
     *
     * @param sendTo  发给谁
     * @param title   标题
     * @param content 内容
     */
    public void sendTo(String sendTo, String title, String content) {
        SimpleMailMessage simpleMailMessage = new SimpleMailMessage();
        //发件人
        simpleMailMessage.setFrom(emailConfig.getUsername());
        //发给谁
        simpleMailMessage.setTo(sendTo);
        //邮件主题
        simpleMailMessage.setSubject(title);
        //邮件内容
        simpleMailMessage.setText(content);
        javaMailSender.send(simpleMailMessage);
    }

    /**
     * 发送带附件的邮件
     *
     * @param sendTo  发给谁
     * @param title   标题
     * @param content 内容
     * @param file    附件
     */
    public void sendAttachmentMail(String sendTo, String title, String content, File file) {
        MimeMessage mimeMessage = javaMailSender.createMimeMessage();
        try {
            MimeMessageHelper message = new MimeMessageHelper(mimeMessage, true);
            //发送给谁
            message.setTo(sendTo);
            //发件人
            message.setFrom(emailConfig.getUsername());
            //标题
            message.setSubject(title);
            //邮件内容
            message.setText(content);
            //附件,参数1：文件名，参数2，文件
            message.addAttachment("刘思晴.jpg", file);
        } catch (MessagingException m) {
            m.printStackTrace();
        }
        javaMailSender.send(mimeMessage);
    }


    public void sendTemplateMail(String sendTo, String title, String templateName) {
        MimeMessage mimeMessage = javaMailSender.createMimeMessage();
        try {
            MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(mimeMessage, true);
            mimeMessageHelper.setFrom(emailConfig.getUsername());
            mimeMessageHelper.setSubject("模板数据");
            mimeMessageHelper.setTo(sendTo);
            final Template template = freeMarkerConfigurer.getConfiguration().getTemplate(templateName);


            Map<String, List<User>> listMap = new HashMap<>();
            List<User> users = new ArrayList<>();
            User liusiqing = new User();
            liusiqing.setAge(17);
            liusiqing.setName("刘思晴");
            liusiqing.setDate(new Date());
            User maosuyu = new User();
            maosuyu.setDate(new Date());
            maosuyu.setName("毛苏雨");
            maosuyu.setAge(18);
            users.add(liusiqing);
            users.add(maosuyu);
            listMap.put("userlist", users);
            Map<String, Map<String, List<User>>> map = new HashMap<>();
            map.put("user", listMap);
            //绑定数据
            final String data = FreeMarkerTemplateUtils.processTemplateIntoString(template, map);


            mimeMessageHelper.setText(data, true);
        } catch (Exception m) {
            m.printStackTrace();
        }
        javaMailSender.send(mimeMessage);
    }
}
